package com.hezhi.trafficcopsys.view.action;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import com.hezhi.framework.Constants;
import com.hezhi.framework.action.BaseAction;
import com.hezhi.framework.exception.APPException;
import com.hezhi.trafficcopsys.domain.SpecialCar;
import com.hezhi.trafficcopsys.service.SpecialCarService;
import com.hezhi.trafficcopsys.service.impl.SpecialCarServiceImpl;
import com.hezhi.trafficcopsys.util.Constants4sys;

public class SpecialCarAction extends BaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = -902360542845042199L;
	
	//注入Service
	private SpecialCarService specialCarService;

	public SpecialCarAction() {
		specialCarService = new SpecialCarServiceImpl();
	}
	
	//form bean
	private SpecialCar specialCar;
	
	// myFile属性用来封装上传的文件  
    private File myFile;

	public SpecialCar getSpecialCar() {
		return specialCar;
	}
	public void setSpecialCar(SpecialCar specialCar) {
		this.specialCar = specialCar;
	}
	
	public File getMyFile() {
		return myFile;
	}
	public void setMyFile(File myFile) {
		this.myFile = myFile;
	}
	/**
	 * 添加特殊车辆
	 * @throws IOException
	 */
	public void doAddSpecialCar() throws IOException{
		try{
			//SpecialCar.setPassword(MD5Util.getMd5(password));
			specialCarService.addSpecialCar(specialCar);
		} catch (Exception e){
			response.setCharacterEncoding(Constants4sys.ENCODING_UTF8);
			response.getWriter().print(e.getMessage());
		}
	}
	
	/**
	 * 编辑选中的特殊车辆
	 * @throws IOException
	 */
	public void doEditSpecialCar() throws IOException{
		try{
			//SpecialCar.setPassword(MD5Util.getMd5(password));
			specialCarService.editSpecialCar(specialCar);
		} catch (APPException e){
			response.setCharacterEncoding(Constants4sys.ENCODING_UTF8);
			response.getWriter().print(e.getMessage());
		}
	}
	
	/**
	 * 删除选中的特殊车辆
	 * @throws IOException
	 */
	public void doDelSpecialCar() throws IOException{
		try {
			String carNo  = request.getParameter("carNo");
			String[] specialCarNos = null;
			if( carNo != null ){
				if( carNo.contains(",")){
					specialCarNos = carNo.split(",");
				} else {
					specialCarNos = new String[1];
					specialCarNos[0] = carNo;
				}
				specialCarService.delSpecialCars(specialCarNos);
			}
		} catch (APPException e) {
			response.setCharacterEncoding(Constants.ENCODING_UTF8);
			response.getWriter().print(e.getMessage());
		}
	}

	/**
	 * 列出所有特殊车辆
	 * @throws IOException
	 */
	public void doLoadSpecialCars() throws IOException {

		try {
			/**
			 * 获取前台数据请求
			 */
			// 页面大小
			int pageSize = Integer.parseInt(request
					.getParameter(Constants4sys.PAGE_SIZE));
			// 当前页
			int pageNo = Integer.parseInt(request
					.getParameter(Constants4sys.PAGE_NO));

			/**
			 * 调用查询方法
			 */
			List<SpecialCar> listSpecialCars = specialCarService.listSpecialCarByPage(pageSize, pageNo);

			/**
			 * 封装Json数据
			 */
			int total = specialCarService.getTotalSize(); // 符合查询的总条数
			if (listSpecialCars == null) {
				// 注：如果从数据库查询不出数据，也必须封装一个空的json集合，不然页面就会报js错误
				listSpecialCars = new ArrayList<SpecialCar>();
			}
			JSONObject datas = new JSONObject();
			// 设置总共有多少条记录
			datas.put(Constants4sys.TOTAL, total);
			// 设置当前页的数据
			datas.put(Constants4sys.PAGE_SIZE, listSpecialCars);

			response.setCharacterEncoding(Constants4sys.ENCODING_UTF8);
			response.getWriter().print(datas.toString());
		} catch (APPException e) {
			response.setCharacterEncoding(Constants.ENCODING_UTF8);
			response.getWriter().print(e.getMessage());
		}
	}
	
	/**
	 * 批量上传特殊车辆
	 * @throws IOException
	 */
	public String doLoadSpecialCarsFromFile() throws IOException {
		try {
			int nums = specialCarService.loadSpecialCarsFromFile(myFile);
		} catch (APPException e) {
			response.setCharacterEncoding(Constants.ENCODING_UTF8);
			response.getWriter().print(e.getMessage());
		}
		return "success";
	}
}
